package cn.xyf.mzmy.common.netty.config;

import cn.xyf.mzmy.common.netty.core.server.NettyWebSocketServer;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class NettyWebSocketConfig {

    @Value("${netty.websocket.port}")
    private int port;

    @Autowired
    private NettyWebSocketServer nettyWebSocketServer;

    @PostConstruct
    public void startNettyServer() {
        new Thread(() -> nettyWebSocketServer.start(port)).start(); // 在单独线程中启动Netty服务器
    }

    @PreDestroy
    public void stopNettyServer() {
        if (nettyWebSocketServer != null) {
            nettyWebSocketServer.stop(); // 应用关闭时停止Netty服务器
        }
    }
}